package com.stenio.fancat.service.baby;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.baby.BabyRelationship;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.common.errorCode.ErrorCode;
import com.stenio.fancat.domain.baby.BabyIconImage;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.mapper.baby.BabyIconImageMapper;
import com.stenio.fancat.mapper.baby.BabyRelationshipMapper;
import com.stenio.fancat.mapper.user.UserRelationshipMapper;
import com.stenio.fancat.service.OssService;
import com.stenio.fancat.util.EmptyUtil;
import com.stenio.fancat.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;


/**
 * baby头像
 */
@Service
public class BabyIconImageService extends ServiceImpl<BabyIconImageMapper, BabyIconImage> {

    @Autowired
    private UserRelationshipMapper userRelationshipMapper;

    @Autowired
    private BabyRelationshipMapper babyRelationshipMapper;

    @Autowired
    private BabyIconImageMapper babyIconImageMapper;

    @Autowired
    private OssService ossService;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${oss.piggybank.endpoint}")
    private String piggybankEndPoint;

    @Value("${key}")
    private String babyKey;


    /**
     * 保存图片到OSS服务器 ---> 数据库
     * @param file
     * @return
     */
    public BabyIconImage insertBabyIconImageByCondition(MultipartFile file, String userId,String babyId) throws FanCatException {
        if (EmptyUtil.isEmpty(userId) || EmptyUtil.isEmpty(file) || EmptyUtil.isEmpty(babyId)) {
            throw new FanCatException("参数异常", ErrorCode.parameters);
        }

        BabyIconImage image = new BabyIconImage();
        Date date = new Date();
        image.setCreateTime(date);
        image.setUpdateTime(date);
        image.setBabyId(babyId);
        try {
            String fileName = ossService.getIconName(file.getOriginalFilename(),babyId);//用babyId当做图片的名字
            image.setImage("https://piggybank-photos.oss-cn-beijing.aliyuncs.com/"+"babyIcon/"+fileName);
            ossService.putObjectSecond("babyIcon/"+fileName,file.getBytes());
            babyIconImageMapper.insert(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 通过userId修改头像图片
     * @param file
     * @param userId
     * @return
     */
    public BabyIconImage modifyBabyIconImageByCondition(MultipartFile file,String userId,String babyId) throws FanCatException {
        if (EmptyUtil.isEmpty(userId) && EmptyUtil.isEmpty(file) || EmptyUtil.isEmpty(babyId)) {
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }

        //删除数据库原有头像的数据
        babyIconImageMapper.deleteByBid(babyId);
        //调用上传头像的方法
        this.insertBabyIconImageByCondition(file,userId,babyId);

        // 查询修改之后的图片
        BabyIconImage iconImage = babyIconImageMapper.selectByBid(babyId);
        iconImage.setId(null);
        iconImage.setUpdateTime(null);
        iconImage.setCreateTime(null);

        //操作Redis
        redisUtil.del(babyKey+babyId+userId);

        return iconImage;
    }


}
